package www.wxwnb.clerk.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import www.wxwnb.clerk.R;
import www.wxwnb.clerk.base.BaseActivity;
import www.wxwnb.clerk.http.HttpMethods;
import www.wxwnb.clerk.http.HttpService;
import www.wxwnb.clerk.http.entity.EditBean;
import www.wxwnb.clerk.http.entity.EditDataBean;
import www.wxwnb.clerk.utils.Customer;

public class EditDataActivity extends BaseActivity {

    private static final String TAG = "EditDataActivity";
    private ImageButton ibtn_back;
    private TextView tv_title;

    private TextView tv_loginName;
    private EditText et_username;
    private EditText et_phoneNum;
    private EditText et_oldPassword;
    private EditText et_rootPassword;
    private EditText et_rootPassword2;
    private TextView tv_jobNum;
    private Button btn_confirm;
    private Button btn_cancel;

    private RadioButton rb_man;
    private RadioButton rb_woman;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_data);
        mContext = this;
    }

    @Override
    protected void initView() {
        ibtn_back = (ImageButton) findViewById(R.id.ibtn_back);
        tv_title = (TextView) findViewById(R.id.app_title);

        tv_loginName = (TextView) findViewById(R.id.tv_loginName);
        et_username = (EditText) findViewById(R.id.et_username);
        et_phoneNum = (EditText) findViewById(R.id.et_phoneNum);

        et_oldPassword = (EditText) findViewById(R.id.et_oldPassword);
        et_rootPassword = (EditText) findViewById(R.id.et_rootPassword);
        et_rootPassword2 = (EditText) findViewById(R.id.et_rootPassword2);

        rb_man = (RadioButton) findViewById(R.id.rb_man);
        rb_woman = (RadioButton) findViewById(R.id.rb_woman);

        tv_jobNum = (TextView) findViewById(R.id.tv_jobNum);
        btn_confirm = (Button) findViewById(R.id.btn_confirm);
        btn_cancel = (Button) findViewById(R.id.btn_cancel);
    }

    @Override
    public void initListener() {
        ibtn_back.setOnClickListener(this);
        btn_confirm.setOnClickListener(this);
        btn_cancel.setOnClickListener(this);
    }

    @Override
    public void initData() {
        tv_title.setText(R.string.edit_data);
        getDataFromServer(Customer.getInstance().getUserId());
    }

    private void getDataFromServer(String userId) {
        HttpService httpService = HttpMethods.getInstance().mRetrofit.create(HttpService.class);
        Call<EditDataBean> call = httpService.getEditData(userId);
        call.enqueue(new Callback<EditDataBean>() {
            @Override
            public void onResponse(Call<EditDataBean> call, Response<EditDataBean> response) {
                if (response.isSuccessful()) {
                    if (response.body().getReturnCode() == 1) {
                        handlerResponseData(response.body().getData());
                    }
                }
            }

            @Override
            public void onFailure(Call<EditDataBean> call, Throwable t) {

            }
        });
    }

    private void handlerResponseData(EditDataBean.DataBean data) {
        tv_loginName.setText(data.getLoginName());
        et_username.setText(data.getName());
        et_username.setSelection(data.getName().length());
        et_phoneNum.setText(data.getPhone());
        tv_jobNum.setText(data.getJobNumber());
//        et_oldPassword.setText(data.getPassword());
        if (data.getSex().equals(getString(R.string.man))) {
            rb_man.setChecked(true);
            rb_woman.setChecked(false);
        } else if (data.getSex().equals(getString(R.string.woman))) {
            rb_man.setChecked(false);
            rb_woman.setChecked(true);
        }
    }

    @Override
    public void processClick(View v) {
        switch (v.getId()) {
            case R.id.ibtn_back:
                finish();
                break;

            case R.id.btn_confirm:
                sendDataToServer(Customer.getInstance().getUserId());
                break;
            case R.id.btn_cancel:
                finish();
                break;
        }
    }

    private void sendDataToServer(String id) {
        String loginName = tv_loginName.getText().toString().trim();
        String username = et_username.getText().toString().trim();
        String sex = "";
        if (rb_man.isChecked()) {
            sex = getString(R.string.man);
        } else if (rb_woman.isChecked()) {
            sex = getString(R.string.woman);
        }

        String phoneNum = et_phoneNum.getText().toString().trim();
        String jobNum = tv_jobNum.getText().toString();

        String oldPassword = et_oldPassword.getText().toString().trim();
        String rootPassword = et_rootPassword.getText().toString().toString().trim();
        String rootPassword2 = et_rootPassword2.getText().toString().trim();

        if (TextUtils.isEmpty(rootPassword) && TextUtils.isEmpty(rootPassword2)) {

        } else if (!TextUtils.isEmpty(rootPassword) && TextUtils.isEmpty(rootPassword2)) {
            Toast.makeText(this, "请填写重新输入", Toast.LENGTH_SHORT).show();
            return;
        } else if (TextUtils.isEmpty(rootPassword) && !TextUtils.isEmpty(rootPassword2)) {
            Toast.makeText(this, "请填写重置密码", Toast.LENGTH_SHORT).show();
            return;
        } else if (!TextUtils.isEmpty(rootPassword) && !TextUtils.isEmpty(rootPassword2)) {
            if (rootPassword.equals(rootPassword2)) {

            } else {
                Toast.makeText(this, "请确认输入的密码是否一致", Toast.LENGTH_SHORT).show();
                return;
            }
        }


        HttpService httpService = HttpMethods.getInstance().mRetrofit.create(HttpService.class);
        Call<EditDataBean> call = httpService.sendEditData(new EditBean(id, loginName, username, sex, phoneNum, "1", jobNum, oldPassword, rootPassword));
        call.enqueue(new Callback<EditDataBean>() {
            @Override
            public void onResponse(Call<EditDataBean> call, Response<EditDataBean> response) {
                if (response.isSuccessful()) {
                    int returnCode = response.body().getReturnCode();
                    String returnMessage = response.body().getReturnMessage();
                    if (returnCode == 1) {
                        finish();
                        Toast.makeText(mContext, returnMessage, Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(mContext, returnMessage, Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void onFailure(Call<EditDataBean> call, Throwable t) {

            }
        });
    }
}
